// pages/photo/photo.js
import {Photo} from './photo-model.js';
var photo = new Photo();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: false,
    swiperIndex: 1,
    commentShow: false,
    imgwidth: 0,
    imgheight: 0,  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    var mine = options.mine;
    this.data.mine = mine;
    this._loadData();
  },

  onShow: function(e) {
    this._loadData();
  },

  delPhotoTap: function(e) {
    var that = this;
    var id = photo.getDataSet(e, 'id');
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          photo.deletePhoto(id, (res) => {
            if (res.code) {
              wx.navigateBack();
            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  previewImage: function (e) {
    var that = this,
      index = that.data.swiperIndex - 1,
      pictures = that.data.pictures;
    wx.previewImage({
      current: pictures[index],
      urls: pictures
    })
  },

  _loadData: function() {
    var that = this;
    photo.getPhotoInfo(this.data.id, (res) => {
      var album = res.data.album;
      var pictures = album.map(function (item, index, input) {
        return item.img;
      });
      this.setData({
        album: res.data.album,
        photoInfo: res.data,
        comments: res.data.comments.data,
        pictures: pictures
      })
    })

    if (that.data.mine == 1) {
      // 修改未读评论状态
      photo.updateComments(that.data.id, (res) => {
         console.log(res)
      })
    }
  },

  onSlideChangeEnd: function (e) {
    var that = this;
    that.setData({
      swiperIndex: e.detail.current + 1
    })
  },  

  deleteTap: function (e) {
    var that = this;
    var id = photo.getDataSet(e, 'id');
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          photo.deleteComment(id, (res) => {
            if (res.code) {
              that._loadData();
            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  replyTap: function(e) {
    var id = photo.getDataSet(e, 'id');
    var nickname = photo.getDataSet(e, 'nickname');

    wx.navigateTo({
      url: '../reply/reply?id=' + id + '&nickname=' + nickname,
    })
  },
  
  toggleComment: function (e) {
    if (!this.data.commentShow) {
      this.setData({
        commentShow: true
      })
    } else {
      this.setData({
        commentShow: false
      })
    }
  },

  formSubmit: function (e) {
    var that = this;
    if (!e.detail.value.content) {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    photo.postComments(e.detail.value, (data) => {
      if (data.code) {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
        this._loadData();
        that.setData({
          form_info: ''
        }) 

      } else {
        wx.showToast({
          title: data.error,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  // imageLoad: function (e) {
  //   var _this = this;
  //   var $width = e.detail.width,    //获取图片真实宽度  
  //     $height = e.detail.height,
  //     ratio = $width / $height;   //图片的真实宽高比例  
  //     var viewWidth = $width * 550 / $height,           //设置图片显示宽度，  
  //       viewHeight = 550;    //计算的高度值
    
         
  //   this.setData({
  //     imgwidth: viewWidth,
  //     imgheight: viewHeight
  //   })
  // }  
  
})